// pages/errorsStoreroom/errorsStoreroom.js
import api from "../../config/api";
import request from "../../utils/request";
import { shareOptions } from "../../utils/access";
const { getBarAndTitleHeight, stringify, parse, getStorage, jump } = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    knowledgeId:'',
    // 题目数据
    storeeroom: [],
    // 当前页面展示的数组
    newStoreeroom:[],
    // 当前下标
    num:0,
    // 当前题目
    newPage: 1,
    // 提交答案按钮的显示
    isShow:false,
    // 用户选项表单
    userForm:[],
    // 传递下一层的数据
    accomplishData:[],
    // 控制C选项的显示隐藏
    isCshow: false,
    // 控制D选项的显示隐藏
    isDshow:false,
    typeList: ['单选','多选','判断','填空'],
    // 状态栏&标题栏高度
    ...getBarAndTitleHeight(true),
    progress: 0,
    currentTitle:'',
    progressValue: 10
  },
  // 跳转答案页面
  async goErrorsAccomplish(){
    if (this.loading) return
    this.loading = true
    // 传输表单
    const data = await request.post(api.sendStoreroomList,{
      studyQuestionRecordDTOList:this.data.userForm
    })
    this.setData({
      accomplishData:data.data
    })
    if (data.httpStatus === 200){ 
      wx.redirectTo({
        url: `/pages/errorsAccomplish/errorsAccomplish?accomplishData=${stringify(this.data.accomplishData)}&knowledgeId=${this.data.knowledgeId}`,
        complete: () => {
          this.loading = false
        }
      })
    } else {
      this.loading = false
    }
  },
  // 随机获得题目
  async getStoreroomData(){
    const { data } = await request.get(api.getStoreroomList,{
      knowledgeId:this.data.knowledgeId
    })
    this.setData({
      storeeroom:data,
      newStoreeroom : data[this.data.num],
      progressValue : 1 / data.length * 100,
    })
  },
  // 点击选项进入下一题
  goTopic(options){
    let { storeeroom, userForm, num, newPage} = this.data
    let { id, option, type } = options?.currentTarget?.dataset
    // 将用户的选择和答案添加入 选项表单
    let user={
      id: id,
      usersAnswer: option,
      type: type
    }
    if (userForm.length < storeeroom.length){
      const addUserFrom = []
      addUserFrom.push(...userForm, user)
      this.setData({
        userForm: addUserFrom
      })
    }
    storeeroom[num]['myAnswer'] = option
    // 判断是否是最后一道题
    // 是 ，显示提交答案按钮，
    // 否 ， 显示下一道题目
    this.setData({
      newStoreeroom: storeeroom[num],
    })

    setTimeout(()=>{
      if (newPage === storeeroom.length){
        return this.goErrorsAccomplish()
      }
      let num1= num + 1
      this.setData({
        isShow: true,
        num: num1,
        newPage: newPage + 1,
        storeeroom: storeeroom,
        newStoreeroom: storeeroom[num1],
        progressValue: (newPage + 1) / storeeroom.length * 100
      })
    },700)
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      knowledgeId:options.knowledgeId,
      accomplishData: [],
      userForm: [],
      isShow: false,
      newPage:1,
      num:0,
      storeeroom:[],
      newStoreeroom:[],
      
      ...getBarAndTitleHeight(),
    })
    this.getStoreroomData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage (e) {
    return shareOptions('default', e)
  }
})